package com.erp.base.business.service;

import java.util.Date;
import java.util.List;

import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.base.base.CrudService;
import com.erp.base.business.dao.CityDao;
import com.erp.base.business.entity.City;
import com.erp.base.common.utils.ShiroUtils;
import com.erp.base.system.entity.User;

@Service
@Transactional(readOnly = true)
public class CityService extends CrudService<CityDao, City, Long> {
	
	@Transactional(readOnly = false)
	@Override
	public void save(City city) {
		User user=ShiroUtils.getUser();
		if (city.getIsNewRecord()){ 
			city.setCreateBy(user.getName());
			city.setCreateDate(new Date());
			city.setUpdateBy(user.getName());
			city.setUpdateDate(new Date());
			dao.insert(city);
		}else{
			city.setUpdateBy(user.getName());
			city.setUpdateDate(new Date());
			int i=dao.update(city);
			if(i==0){
				throw new OptimisticLockingFailureException("乐观锁错误!") ;
			}
		}
	}
	public City findCityByCityName(String cityName){
		City city=new City();
		city.setCityName(cityName);
		List<City> cityList = dao.findList(city);
		if(cityList!=null&&!cityList.isEmpty()){
			return cityList.get(0);
		}
		return null;
	}
}
